﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace frmCheckStock
{
    public partial class frmStockAdjustment : Form
    {
        LogicUniversityT2Entities logic = new LogicUniversityT2Entities();
        public frmStockAdjustment()
        {
            InitializeComponent();
        }

        private void frmStockAdjustment_Load(object sender, EventArgs e)
        {
            lblTodayDate.Text = DateTime.Today.ToString();
            var adjustmentList = from adjust in logic.AdjustmentVouchers
                                 join item in logic.Items on adjust.ItemID equals item.ItemID
                                 where adjust.Status == "Pending"                           
                                 select new
                                 {
                                    Items_ID = adjust.ItemID,
                                    Item_Description = item.ItemDescription,
                                    SystemStockBalance = item.StockBalance                         
                                 };
            var adjustmentlist = adjustmentList.ToList();
            gvAdjustmentItemList.DataSource = adjustmentlist;
        }

        private void btnRequestVoucher_Click(object sender, EventArgs e)
        {
            int lostQuantity=0;
            int actstock = 0;
            for (int i = 0; i < gvAdjustmentItemList.Rows.Count; i++)
            {
                DataGridViewTextBoxCell gvtxtboxSysStock= new DataGridViewTextBoxCell();
                gvtxtboxSysStock = (DataGridViewTextBoxCell)gvAdjustmentItemList.Rows[i].Cells[4];
                int sysStock = Convert.ToInt32(gvtxtboxSysStock.Value.ToString());

                DataGridViewTextBoxCell gvtxtboxActStock = new DataGridViewTextBoxCell();
                gvtxtboxActStock = (DataGridViewTextBoxCell)gvAdjustmentItemList.Rows[i].Cells[0];
                if(gvtxtboxActStock.Value!=null)
                    actstock = Convert.ToInt32(gvtxtboxActStock.Value.ToString());

                if(sysStock!=0 && actstock !=0)
                {
                    lostQuantity = sysStock - actstock;
                    actstock = 0;
                    DataGridViewTextBoxCell gvtxtboxItemID = new DataGridViewTextBoxCell();
                    gvtxtboxItemID = (DataGridViewTextBoxCell)gvAdjustmentItemList.Rows[i].Cells[2];
                    string itemID = gvtxtboxItemID.Value.ToString();

                    DataGridViewTextBoxCell gvtxtboxRemark = new DataGridViewTextBoxCell();
                    gvtxtboxRemark = (DataGridViewTextBoxCell)gvAdjustmentItemList.Rows[i].Cells[1];
                    string remark = gvtxtboxRemark.Value.ToString();

                    AdjustmentVoucher adjVoucher = logic.AdjustmentVouchers.First(adjItem => adjItem.ItemID == itemID);
                    adjVoucher.Quantity = lostQuantity;
                    adjVoucher.Remarks = remark;
                    logic.SaveChanges();
                }
            }
        }
    }
}
